<template>
  <el-dialog :close-on-click-modal="false" 
    :title="title"
    :visible="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="RoleName">
        <el-input v-model="form.RoleName" />
      </el-form-item>
      <el-form-item label="备注" prop="Remarks">
        <el-input v-model="form.Remarks" />
      </el-form-item>
      <el-form-item label="菜单">
        <div class="vab-tree-border">
          <el-tree
            ref="tree"
            :data="list"
            :default-checked-keys="[
              '/',
              '/vab',
              '/other',
              '/mall',
              '/setting',
              '/error',
            ]"
            :default-expanded-keys="[]"
            node-key="path"
            show-checkbox
          >
            <template #default="{ data }">
              <span>{{ data.meta.title }}</span>
            </template>
          </el-tree>
        </div>
      </el-form-item>
      <!-- <el-form-item label="按钮权限">
        <el-checkbox-group v-model="form.btnRolesCheckedList">
          <el-checkbox
            v-for="item in btnRoles"
            :key="item.value"
            :label="item.value"
          >
            {{ item.lable }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { RNewly, RRenew } from '@/api/roleManagement'
  import routData from './data'

  export default {
    name: 'RoleManagementEdit',
    data() {
      return {
        form: {
          btnRolesCheckedList: [],
        },
        rules: {
          role: [{ required: true, trigger: 'blur', message: '请输入角色码' }],
        },
        title: '',
        dialogFormVisible: false,
        firstList: [
          '/',
          '/equipment',
          '/product',
          '/material',
          '/production',
          '/quality',
          '/setting',
        ],
        list: [],
        /* btnRoles demo */
        btnRoles: [
          {
            lable: '读',
            value: 'read:system',
          },
          {
            lable: '写',
            value: 'write:system',
          },
          {
            lable: '删',
            value: 'delete:system',
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      async fetchData() {
        this.list = routData
      },
      save() {
        // 具体业务请自行调整
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const tree = this.$refs.tree.getCheckedKeys()
            const treeObject = { 'treeArray:': tree }
            let Authority = []
            let list1 = []
            let list = this.$refs.tree.getCheckedNodes()
            list.forEach((item) => {
              console.log(this.firstList.indexOf(item.path) > -1, item)
              // let inde
              if (this.firstList.indexOf(item.path) > -1) {
                //是否是第一层
                if (list1.indexOf(item.path) == -1) {
                  //是否已经加入权限的数组中了
                  list1.push(item.path)
                  let obj = {
                    Firstlevelpage: item.meta.title,
                    Secondarypage: [],
                  }
                  Authority.push(obj)
                }
              } else {
                //第二层
                this.list.forEach((ii) => {
                  console.log('iiii', ii)
                  ii.children.forEach((iii) => {
                    if (iii.path == item.path) {
                      //找到第二层的位置
                      console.log(list1, ii.path, iii.path, Authority)
                      let key = list1.indexOf(ii.path) //找到第一层在权限中的位置
                      if (key > -1) {
                        Authority[key].Secondarypage.push(iii.meta.title)
                      } else {
                        list1.push(ii.path)
                        let obj = {
                          Firstlevelpage: item.meta.title,
                          Secondarypage: [iii.meta.title],
                        }
                        Authority.push(obj)
                      }
                    }
                  })
                })
              }
            })
            if (this.title == '添加') {
              let user = JSON.parse(localStorage.getItem('UserInfo'))
              let param = {
                RoleName: this.form.RoleName,
                Remarks: this.form.Remarks,
                Authority: Authority,
                Creator: user.UserName,
                CreateTime: this.days(),
              }
              await RNewly(param)
            } else {
              let param = {
                RoleName: this.form.RoleName,
                Remarks: this.form.Remarks,
                Authority: Authority,
                Creator: this.form.Creator,
                CreateTime: this.form.CreateTime,
              }
              await RRenew(param)
            }
            this.$baseMessage('操作成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .vab-tree-border {
    height: 200px;
    padding: $base-padding;
    overflow-y: auto;
    border: 1px solid #dcdfe6;
    border-radius: $base-border-radius;
  }
</style>
